package logic

import (
	"context"
	"encoding/json"
	"furni-set/models/cache"
	"furni-set/models/mysql"
	"time"

	"furni-set/furnirpc/furnirpc"
	"furni-set/furnirpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseListLogic {
	return &HouseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 家具详情
func (l *HouseListLogic) HouseList(in *furnirpc.HouseListRequest) (*furnirpc.HouseListResponse, error) {
	// todo: add your logic here and delete this line

	var houseList []*furnirpc.HouseList

	result, _ := cache.Rdb.Get(l.ctx, "list").Result()

	if result == "" {
		houseList = mysql.HouseList(mysql.DB, houseList)

		marshal, _ := json.Marshal(houseList)

		cache.Rdb.Set(l.ctx, "list", marshal, time.Minute*30)
	} else {
		json.Unmarshal([]byte(result), &houseList)
	}

	return &furnirpc.HouseListResponse{
		List: houseList,
	}, nil
}
